package designer.observer.impl;

import designer.observer.inter.IObserver;
import designer.observer.inter.ISubject;

import java.util.ArrayList;
import java.util.List;

/**
 * 阿里的offer模拟
 * Created by tanlee on 2016/4/19.
 */
public class AlibabaOffer implements ISubject {

    private List<IObserver> observers = new ArrayList<IObserver>();
    private String msg; // 通知的消息
    @Override
    public void registerObserver(IObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(IObserver observer) {
        int index = observers.indexOf(observer);
        if(index >= 0){
            observers.remove(index);
        }
    }

    @Override
    public void notifyObserver() {
        for(IObserver observer:observers){
            observer.update(msg);
        }
    }

    /**
     * 更新offer消息
     */
    public void setMsg(String msg){
        this.msg = msg;
        notifyObserver();
    }
}
